//
//  AppDelegate.m
//  B_Project
//
//  Created by 胡佳俊 on 15/11/23.
//  Copyright © 2015年 HJJ. All rights reserved.
//

#import "AppDelegate.h"
#import "HZL_SettingViewController.h"
#import "HZL_FirstMainViewController.h"
#import "HZL_SecondMainViewController.h"
#import "HZL_RecommendViewController.h"
#import "HZL_FourthMainViewController.h"
#import "EaseMob.h"
#import "APService.h"


@interface AppDelegate ()<UITabBarControllerDelegate, UIScrollViewDelegate>
@property (nonatomic, retain) UIScrollView *imageScrollView;
@property (nonatomic, retain) UIImageView *images;
@end

@implementation AppDelegate


- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    // Override point for customization after application launch.
    self.window.backgroundColor = [UIColor whiteColor];
    [self.window makeKeyAndVisible];
    
    
    // registerSDKWithAppKey:注册的appKey
    // apnsCertName:推送证书名(不需要加后缀)
    [[EaseMob sharedInstance] registerSDKWithAppKey:@"825428416#followme" apnsCertName:nil];
    [[EaseMob sharedInstance] application:application didFinishLaunchingWithOptions:launchOptions];
    
    // 第1个
    HZL_FirstMainViewController *firstVC = [[HZL_FirstMainViewController alloc] init];
    UINavigationController *navi1 = [[UINavigationController alloc] initWithRootViewController:firstVC];
    firstVC.tabBarItem = [[UITabBarItem alloc] initWithTitle:@"精选" image:[UIImage imageNamed:@"iconfont-jingxuanfuzhifuzhi_2"] tag:1000];
    
    
    // 第2个
    HZL_SecondMainViewController *secondVC = [[HZL_SecondMainViewController alloc] init];
    UINavigationController *navi2 = [[UINavigationController alloc] initWithRootViewController:secondVC];
    secondVC.tabBarItem = [[UITabBarItem alloc] initWithTitle:@"发现" image:[UIImage imageNamed:@"iconfont-faxian_2"] tag:2000];
    
    // 第3个
    HZL_RecommendViewController *thirdVC = [[HZL_RecommendViewController alloc] init];
    UINavigationController *navi3 = [[UINavigationController alloc] initWithRootViewController:thirdVC];
    thirdVC.tabBarItem = [[UITabBarItem alloc] initWithTitle:@"推荐" image:[UIImage imageNamed:@"iconfont-tuijianb_2"] tag:3000];
    
//    // 第4个
//    HZL_FourthMainViewController *fourthVC = [[HZL_FourthMainViewController alloc] init];
//    UINavigationController *navi4 = [[UINavigationController alloc] initWithRootViewController:fourthVC];
//    fourthVC.tabBarItem = [[UITabBarItem alloc] initWithTitle:@"4" image:[UIImage imageNamed:@"产品"] tag:4000];
    
    self.tab = [[UITabBarController alloc] init];
    self.tab.viewControllers = @[navi3, navi1, navi2];
    self.tab.tabBar.translucent = NO;
    self.tab.delegate = self;
    
    HZL_SettingViewController *sVC = [[HZL_SettingViewController alloc] init];
    RESideMenu *sideMenuViewController = [[RESideMenu alloc] initWithContentViewController:self.tab leftMenuViewController:sVC rightMenuViewController:nil];
    sideMenuViewController.backgroundImage = [UIImage imageNamed:@"beijing"];
    sideMenuViewController.menuPreferredStatusBarStyle = 1; // UIStatusBarStyleLightContent
    sideMenuViewController.delegate = self;
//    sideMenuViewController.contentViewShadowColor = [UIColor blackColor];
//    sideMenuViewController.contentViewShadowOffset = CGSizeMake(0, 0);
//    sideMenuViewController.contentViewShadowOpacity = 0.6;
//    sideMenuViewController.contentViewShadowRadius = 12;
//    sideMenuViewController.contentViewShadowEnabled = YES;
    self.window.rootViewController = sideMenuViewController;
    
#pragma mark - 引导页
    if ([[NSUserDefaults standardUserDefaults]boolForKey:@"firstLaunch"]== NO)
    {
        //判断程序是否第一次启动
        [[NSUserDefaults standardUserDefaults]setBool:YES forKey:@"firstLaunch"];
        //第一次启动操作
        self.imageScrollView = [[UIScrollView alloc] initWithFrame:self.window.frame];
        self.imageScrollView.backgroundColor = [UIColor whiteColor];
        self.imageScrollView.delegate = self;
        [self.imageScrollView setContentSize:CGSizeMake(SCREEN_WIDTH * 3, 0)];
        [self.imageScrollView setPagingEnabled:YES];
        [self.imageScrollView showsHorizontalScrollIndicator];
        [self.imageScrollView setBounces:NO];
        
        NSArray *imagesArray = @[@"a1",@"a1",@"a1"];
        for (int i = 0; i < 3; i++) {
            self.images =[[UIImageView alloc]initWithFrame:CGRectMake(i *SCREEN_WIDTH, 0, SCREEN_WIDTH, SCREEN_HEIGHT)];
            [self.images setImage:[UIImage imageNamed:[imagesArray objectAtIndex:i]]];
            [self.imageScrollView addSubview:self.images];
        }
        
//        UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(2 *SCREEN_WIDTH + 30, SCREEN_HEIGHT *0.6, 200, 40)];
//        label.text = @"用有趣定义生活";
//        label.textColor = [UIColor colorWithRed:0.34 green:0.7 blue:0.75 alpha:1];
//        label.font = [UIFont boldSystemFontOfSize:25];
//        [self.imageScrollView addSubview:label];
        
        UIButton *button =[UIButton buttonWithType:UIButtonTypeCustom];
        [button setFrame:CGRectMake(2 *SCREEN_WIDTH+SCREEN_WIDTH/2-60,  SCREEN_HEIGHT * 0.75 + 60,120, 40)];
        [button setTitle:@"立即体验" forState:UIControlStateNormal];
        [button.titleLabel setFont:[UIFont systemFontOfSize:22.]];
        [button setTitleColor:[UIColor clearColor] forState:UIControlStateNormal];
        button.layer.cornerRadius = 5;
        button.layer.borderWidth = .5;
        button.layer.masksToBounds = YES;
        //        button.layer.borderColor = [[UIColor colorWithRed:0.34 green:0.7 blue:0.75 alpha:1] CGColor ];
        //        [button setTitleColor: [UIColor colorWithRed:0.34 green:0.7 blue:0.75 alpha:1] forState:UIControlStateNormal];
        [button addTarget:self action:@selector(buttonClick) forControlEvents:UIControlEventTouchUpInside];
        [self.imageScrollView addSubview:button];
        
        [self.window addSubview:self.imageScrollView];
    }
    
    // Required
    if ([[UIDevice currentDevice].systemVersion floatValue] >= 8.0) {
        //可以添加自定义categories
        [APService registerForRemoteNotificationTypes:(UIUserNotificationTypeBadge |
                                                       UIUserNotificationTypeSound |
                                                       UIUserNotificationTypeAlert)
                                           categories:nil];
    } else {
        //categories 必须为nil
        [APService registerForRemoteNotificationTypes:(UIRemoteNotificationTypeBadge |
                                                       UIRemoteNotificationTypeSound |
                                                       UIRemoteNotificationTypeAlert)
                                           categories:nil];
    }
    
    // Required
    [APService setupWithOption:launchOptions];

    return YES;
}

- (void)buttonClick
{
    [UIView animateWithDuration:1 animations:^{
        self.imageScrollView.alpha = 0;
    } completion:^(BOOL finished) {
        self.imageScrollView.hidden = YES;
    }];
}
- (void)applicationWillResignActive:(UIApplication *)application {
    // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
    // Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.
}

// App进入后台
- (void)applicationDidEnterBackground:(UIApplication *)application {
    
    [[EaseMob sharedInstance] applicationDidEnterBackground:application];
    // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
    // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
}
// App将要从后台返回
- (void)applicationWillEnterForeground:(UIApplication *)application {
    // Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background.
    //进入前台取消应用消息图标
    [[UIApplication sharedApplication] setApplicationIconBadgeNumber:0];
    
    [[EaseMob sharedInstance] applicationWillEnterForeground:application];
}
- (void)applicationDidBecomeActive:(UIApplication *)application {
    // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
}
// 申请处理时间
- (void)applicationWillTerminate:(UIApplication *)application {
    [[EaseMob sharedInstance] applicationWillTerminate:application];
    // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
}
- (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken {
    
    // Required
    [APService registerDeviceToken:deviceToken];
}

- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo {
    
    // Required
    [APService handleRemoteNotification:userInfo];
}

- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler {
    
    
    // IOS 7 Support Required
    [APService handleRemoteNotification:userInfo];
    completionHandler(UIBackgroundFetchResultNewData);
}


@end
